package com.lance.answer.gui.answer.gk.answer.lms.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.dto.CompleteInfo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;

public class CountMoneyUtil {

    public static void main(String[] args) throws Exception {


     //   validLms();
    //    countLmsByTeacher();

        // 8748  +1630（一网一）+2961(一体化)
        //  countGk();

     //   validGk();
    //    countGk();
     //   Thread.sleep(6000);
    //    countGkByTeacher();

     //   Thread.sleep(2000000);
        countYiTihua();

          validYitua();
      //   Thread.sleep(2000000);
        //    countUnComplete();


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("刘俊伟");
        // TODO 毛概的都没做
        teachers.add("李明");
        teachers.add("朱晓玉");
        teachers.add("宋柳萌");
        teachers.add("零散");

        int sum = 0;
        for (String teacher : teachers) {
            sum += countYiTihuaByTeacher(teacher);
        }
        System.err.println("sum:"+sum);

        //   count2();


    }

    static void countGk() throws Exception {


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("刘");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("刘俊伟");
        // TODO 毛概的都没做
        teachers.add("李明");
        teachers.add("朱晓玉");
        teachers.add("宋柳萌");
        teachers.add("零散");

        Set<String> set2 = Sets.newHashSet();
        StringBuilder sb = new StringBuilder();
        // key = user
        Map<String, Set<String>> map2 = Maps.newHashMap();
        for (File directory1 : new File("libs\\answerInfo").listFiles()) {
            //     for(File directory:new File("libsTmp\\answerInfo").listFiles()){

            String directoryName = directory1.getName();
            if (!teachers.contains(directoryName)) {
                continue;
            }
            System.err.println("dName:" + directoryName);

            for (File directory2 : directory1.listFiles()) {

                for (File file : directory2.listFiles()) {
                    String REGEX = "[^(0-9)]";
                    String fileName = file.getName();
                    String userName = Pattern.compile(REGEX).matcher(fileName).replaceAll("").trim();
                    String dName = directory2.getName();
                    set2.add(dName + userName);
                    Set<String> courseSet = map2.computeIfAbsent(userName, k -> Sets.newHashSet());
                    courseSet.add(dName);
                }
            }
        }
        for (String s : set2) {
            sb.append(s).append("\r\n");
        }

        System.err.println("courseSet:" + set2.size());

        String fName2 = "f://tmp//国开完成情况.xlsx";
        List<List<String>> userInfo2 = Lists.newArrayList();
        for (String userName : map2.keySet()) {
            List<String> infoAll = Lists.newArrayList();
            infoAll.add(userName);
            infoAll.addAll(map2.get(userName));
            if (infoAll.size() > 0) {
                userInfo2.add(infoAll);
            }
        }
        //  System.err.println("userInfosize:" + userInfo2.size() + JSON.toJSONString(userInfo2));
        EasyExcel.write(fName2)
                // 这里放入动态头
                .head(head()).sheet("学生情况统计")
                // 当然这里数据也可以用 List<List<String>> 去传入
                .doWrite(userInfo2);
        FileUtils.write(new File("f://tmp//国开完成情况.txt"), JSONObject.toJSONString(map2, SerializerFeature.PrettyFormat), "utf-8");
    }


    static void countGkByTeacher() throws Exception {


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("刘俊伟");
        teachers.add("刘");
        teachers.add("李明");
        teachers.add("朱晓玉");
        teachers.add("宋柳萌");
        teachers.add("零散");

        for (String teacher : teachers) {
            Set<String> set2 = Sets.newHashSet();
            StringBuilder sb = new StringBuilder();
            // key = user
            Map<String, Set<String>> map2 = Maps.newHashMap();
            for (File directory1 : new File("libs\\answerInfo").listFiles()) {

                String directoryName = directory1.getName();
                if (!teacher.equals(directoryName)) {
                    continue;
                }

                for (File directory2 : directory1.listFiles()) {

                    for (File file : directory2.listFiles()) {
                        String REGEX = "[^(0-9)]";
                        String fileName = file.getName();
                        String userName = Pattern.compile(REGEX).matcher(fileName).replaceAll("").trim();
                        String dName = directory2.getName();
                        set2.add(dName + userName);
                        Set<String> courseSet = map2.computeIfAbsent(userName, k -> Sets.newHashSet());
                        courseSet.add(dName);
                    }
                }
            }
            for (String s : set2) {
                sb.append(s).append("\r\n");
            }

            System.err.println(teacher + "门数:" + set2.size());

            String fName2 = "f://tmp//国开2022//国开完成情况-" + teacher + ".xlsx";
            List<List<String>> userInfo2 = Lists.newArrayList();
            for (String userName : map2.keySet()) {
                List<String> infoAll = Lists.newArrayList();
                infoAll.add(userName);
                infoAll.addAll(map2.get(userName));
                if (infoAll.size() > 0) {
                    userInfo2.add(infoAll);
                }
            }
            EasyExcel.write(fName2)
                    .head(head()).sheet("学生情况统计")
                    .doWrite(userInfo2);
            FileUtils.write(new File("f://tmp//国开2022//国开完成情况-" + teacher + ".txt"), JSONObject.toJSONString(map2, SerializerFeature.PrettyFormat), "utf-8");
        }


    }

    static void countLmsByTeacher() throws Exception {


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘俊伟");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("零散");

        for (String teacher : teachers) {

            String rootPath = "libs//lmsTeachers//";

            Set<String> tUser = Sets.newHashSet();

            InputStream inputStream2 = new FileInputStream(rootPath + "//tmp//" + teacher + "//" + teacher + "-新国开.txt");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[1];
                tUser.add(userCode.toUpperCase(Locale.ROOT));
            }


            Set<String> set2 = Sets.newHashSet();
            StringBuilder sb = new StringBuilder();
            // key = user
            Map<String, Set<String>> map2 = Maps.newHashMap();
            for (File directory1 : new File("libs\\lmsAnswerInfo").listFiles()) {

                // 课程名称
                String subjectName = directory1.getName();


                    for (File file : directory1.listFiles()) {

                        InputStream completeInfoStream = new FileInputStream(file);
                        String completeInfoStr = IOUtils.toString(completeInfoStream, "UTF-8");
                        List<CompleteInfo> completeInfoList;
                        try {
                            completeInfoList = JSONObject.parseArray(completeInfoStr, CompleteInfo.class);
                            if (null == completeInfoList) {
                                completeInfoList = Lists.newArrayList();
                            }
                        } catch (Exception e) {
                            completeInfoList = Lists.newArrayList();
                        }
                        boolean isGoOn = true;
                        for(CompleteInfo completeInfo:completeInfoList){
                            if("该课程没有形考任务".equals(completeInfo.getSummary())){
                                isGoOn = false;
                            }
                        }
                        if(!isGoOn){
                            continue;
                        }
                        String REGEX = "[^(0-9)]";
                        String fileName = file.getName();
                        String userName = Pattern.compile(REGEX).matcher(fileName).replaceAll("").trim();

                        if(!tUser.contains(userName)){
                            continue;
                        }

                        String dName = directory1.getName();
                        set2.add(dName + userName);
                        Set<String> courseSet = map2.computeIfAbsent(userName, k -> Sets.newHashSet());
                        courseSet.add(dName);
                    }
            }
            for (String s : set2) {
                sb.append(s).append("\r\n");
            }

            System.err.println(teacher + "门数:" + set2.size());

            String fName2 = "f://tmp//一网一2022//国开完成情况-" + teacher + ".xlsx";
            List<List<String>> userInfo2 = Lists.newArrayList();
            for (String userName : map2.keySet()) {
                List<String> infoAll = Lists.newArrayList();
                infoAll.add(userName);
                infoAll.addAll(map2.get(userName));
                if (infoAll.size() > 0) {
                    userInfo2.add(infoAll);
                }
            }
            EasyExcel.write(fName2)
                    .head(head()).sheet("学生情况统计")
                    .doWrite(userInfo2);
            FileUtils.write(new File("f://tmp//一网一2022//国开完成情况-" + teacher + ".txt"), JSONObject.toJSONString(map2, SerializerFeature.PrettyFormat), "utf-8");
        }


    }

    public static void countUnComplete() throws Exception {

        int sum = 0;
        Set<String> courseSet = Sets.newHashSet();

        for (File file : new File("E:\\xjStudentInfo2022\\unDone").listFiles()) {
            String fileName = file.getName();
            fileName = fileName.substring(0, fileName.length() - 5);
            System.err.println(fileName + "," + fileName);
        }

        Thread.sleep(2000000);


        for (File file : new File("E:\\xjStudentInfo2022\\unDone").listFiles()) {

            List<String> listSituation = JSONObject.parseArray(IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8), String.class);

            for (String situation : listSituation) {
                //  String s = situation.substring(6);
                String s = situation;
                s = s.replaceAll("学习未完成", "");
                sum++;
                courseSet.add(s);
            }
        }

        for (String s : courseSet) {
            System.err.println(s);
        }

        System.err.println("sum:" + sum);
    }

    public static void countYiTihua() throws Exception {
        Map<String, Set<String>> map = Maps.newHashMap();

        int sum = 0;

        for (File file : new File("E:\\xjStudentInfo2022\\deal").listFiles()) {

            List<String> listSituation = JSONObject.parseArray(IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8), String.class);

            Set<String> courseSet = Sets.newHashSet();
            for (String situation : listSituation) {
                String s = situation.substring(6);
                s = s.replaceAll("学习完成", "");
                courseSet.add(s);
            }
            sum += courseSet.size();
            String fffName = file.getName();
            map.put(fffName.substring(0, fffName.length() - 5), courseSet);
        }

        System.out.println("sum:::::::" + sum);


        FileUtils.write(new File("f://tmp//一体化完成情况.txt"), JSONObject.toJSONString(map, SerializerFeature.PrettyFormat), "utf-8");
        String fName = "f://tmp//一体化情况.xlsx";
        List<List<String>> userInfo = Lists.newArrayList();
        for (String userName : map.keySet()) {
            List<String> infoAll = Lists.newArrayList();
            infoAll.add(userName);
            infoAll.addAll(map.get(userName));
            if (infoAll.size() > 0) {
                userInfo.add(infoAll);
            }
        }
        EasyExcel.write(fName)
                .head(head()).sheet("学生情况统计")
                .doWrite(userInfo);
        FileUtils.write(new File("f://tmp//一体化情况.txt"), JSONObject.toJSONString(map, SerializerFeature.PrettyFormat), "utf-8");
    }


    public static int countYiTihuaByTeacher(String teacher) throws Exception {


        Set<String> tUser = Sets.newHashSet();

        String rootPath = "C:\\Users\\Administrator\\Desktop\\老师名单\\";
        InputStream inputStream2 = new FileInputStream(rootPath + teacher + "//" + teacher + "-一体化.txt");

        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");
        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[2];
            tUser.add(userCode.toUpperCase(Locale.ROOT).trim());
        }

        Map<String, Set<String>> map = Maps.newHashMap();

        int sum = 0;

        for (File file : new File("E:\\xjStudentInfo2022\\deal").listFiles()) {

            String fileName = file.getName();
            String user = fileName.substring(0, fileName.length() - 5);
            if (!tUser.contains(user.toUpperCase(Locale.ROOT))) {
                continue;
            }
            List<String> listSituation = JSONObject.parseArray(IOUtils.toString(new FileInputStream(file), StandardCharsets.UTF_8), String.class);
            Set<String> courseSet = Sets.newHashSet();
            for (String situation : listSituation) {
                String s = situation.substring(6);
                s = s.replaceAll("学习完成", "");
                courseSet.add(s);
            }
            sum += courseSet.size();
            String fffName = file.getName();
            map.put(fffName.substring(0, fffName.length() - 5), courseSet);
        }

        System.out.println(teacher + "门数:" + sum);


        FileUtils.write(new File("f://tmp//一体化2022//一体化完成情况" + teacher + ".txt"), JSONObject.toJSONString(map, SerializerFeature.PrettyFormat), "utf-8");
        String fName = "f://tmp//一体化2022//一体化情况" + teacher + ".xlsx";
        List<List<String>> userInfo = Lists.newArrayList();
        for (String userName : map.keySet()) {
            List<String> infoAll = Lists.newArrayList();
            infoAll.add(userName);
            infoAll.addAll(map.get(userName));
            if (infoAll.size() > 0) {
                userInfo.add(infoAll);
            }
        }
        EasyExcel.write(fName)
                .head(head()).sheet("学生情况统计")
                .doWrite(userInfo);
        FileUtils.write(new File("f://tmp//一体化2022//" + "一体化情况" + teacher + ".txt"), JSONObject.toJSONString(map, SerializerFeature.PrettyFormat), "utf-8");
    return sum;
    }


    public static void validYitua() throws Exception {


        InputStream inputStream = new FileInputStream("E:\\alluser.txt");
        List<String> allUserString = IOUtils.readLines(inputStream, "utf-8");

        Set<String> userSet = Sets.newHashSet();
        for (String user : allUserString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            userSet.add(userCode.toUpperCase(Locale.ROOT));
        }

        for (File file : new File("E:\\xjStudentInfo2022\\deal").listFiles()) {
            String u = file.getName().substring(0,file.getName().length()-5);
            userSet.add(u.toUpperCase(Locale.ROOT));
        }



        Set<String> tUser = Sets.newHashSet();
        String rootPath = "C:\\Users\\Administrator\\Desktop\\老师名单\\";

        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("刘俊伟");
        teachers.add("李明");
        teachers.add("朱晓玉");
        teachers.add("宋柳萌");
        teachers.add("零散");


        for (String teacher : teachers) {


            InputStream inputStream2 = new FileInputStream(rootPath + teacher + "//" + teacher + "-一体化.txt");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[2];
                tUser.add(userCode.toUpperCase(Locale.ROOT));
            }

        }

        for (String user : userSet) {
            if (!tUser.contains(user)) {
                System.err.println("不在大名单内：" + user);
            }
        }

//        for (String user : tUser) {
//            if (!userSet.contains(user)) {
//                System.err.println("不在大名单内：" + user);
//            }
//        }


    }


    public static void validLms() throws Exception {


        InputStream inputStream = new FileInputStream("libs//lmsTeachers//all3.user");
        List<String> allUserString = IOUtils.readLines(inputStream, "utf-8");

        Set<String> userSet = Sets.newHashSet();
        for (String user : allUserString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            userSet.add(userCode.toUpperCase(Locale.ROOT));
        }

        Set<String> tUser = Sets.newHashSet();
        String rootPath = "libs//lmsTeachers//";

        List<String> teachers = Lists.newArrayList();
        teachers.add("刘俊伟");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("零散");


        for (String teacher : teachers) {

            InputStream inputStream2 = new FileInputStream(rootPath + "//tmp//" + teacher + "//" + teacher + "-新国开.txt");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[1];
                System.err.println("userCode:" + userCode);
                tUser.add(userCode.toUpperCase(Locale.ROOT));
            }

        }

        for (String user : userSet) {
            if (!tUser.contains(user)) {
                System.err.println("不在大名单内：" + user);
            }
        }


    }


    // 检查国开的人数
    public static void validGk() throws Exception {


        List<String> teachers = Lists.newArrayList();
        teachers.add("刘庆");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("刘俊伟");
        // TODO 毛概的都没做
        teachers.add("李明");
        teachers.add("朱晓玉");
        teachers.add("宋柳萌");
        teachers.add("零散");


        for (String teacher : teachers) {

            InputStream inputStream = new FileInputStream("libs//teachers//" + teacher + ".user");
            List<String> allUserString = IOUtils.readLines(inputStream, "utf-8");

            Set<String> userSet = Sets.newHashSet();
            for (String user : allUserString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                userSet.add(userCode.toUpperCase(Locale.ROOT));
            }


            Set<String> tUser = Sets.newHashSet();
            String rootPath = "C:\\Users\\Administrator\\Desktop\\老师名单\\";

            InputStream inputStream2 = new FileInputStream(rootPath + teacher + "//" + teacher + "-旧国开.txt");
            List<String> userString = IOUtils.readLines(inputStream2, "utf-8");
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[1];
                tUser.add(userCode.toUpperCase(Locale.ROOT));
            }

            for (String user : userSet) {
                if (!tUser.contains(user)) {
                    System.err.println("不在大名单内：" + teacher + user);
                }
            }

        }


    }


    private static List<List<String>> head() {
        List<List<String>> list = new ArrayList<>();
        List<String> head0 = new ArrayList<>();
        head0.add("学号");
        list.add(head0);
        for (int i = 0; i < 15; i++) {
            List<String> headI = new ArrayList<>();
            headI.add("课程" + i);
            list.add(headI);
        }
        return list;
    }


    // 统计心理健康这门课
    public static void count2() throws Exception {

        Set<String> users = Sets.newHashSet();
        for (File file : new File("E:\\answerInfo一体化\\心理健康").listFiles()) {
            String fName = file.getName();
            if (!fName.contains("-study")) {
                continue;
            }
            InputStream inputStream = new FileInputStream(file);
            String completeInfoStr = IOUtils.toString(inputStream, "UTF-8");

            List<CompleteInfo> completeInfoList = JSONObject.parseArray(completeInfoStr, CompleteInfo.class);
            for (CompleteInfo info : completeInfoList) {
                if ("完成了这门课程！".equals(info.getSummary())) {
                    users.add(fName.substring(0, fName.length() - 10));
                }
            }
        }
        System.err.println("users:" + users.size());

        for (String u : users) {
            System.err.println(u);
        }

    }

}
